/**
 * 通过日志输出实现chain of responsibility 
 */

class AbstractLogger{
    constructor(){
        if(new.target==AbstractLogger){
            throw new Error('can declare an AbstractLogger object by new ')
        }
    }
    write(){}
    setNext(logger){
        this.next = logger;
    }
    logMsg(level,msg){
        if(level>=this.level){
            this.write(msg)
        }
        if(this.next){
            this.next.logMsg(level,msg)
        }
    }
}

class InfoLogger extends AbstractLogger{
    constructor(){
        super()
        this.level = 1
    }
    write(msg){
        console.log('这里是info控制台',msg)
    }
}

class DebugLogger extends AbstractLogger{
    constructor(){
        super()
        this.level = 2
    }
    write(msg){
        console.debug('这里是debug控制台',msg)
    }
}
class ErrorLogger extends AbstractLogger{
    constructor(){
        super()
        this.level = 3
    }
    write(msg){
        console.error('这里是error控制台',msg)
    }
}
//职责链就是一个链表，里边有一个递归调用下一个的函数
//将消息传递给链表的第一个节点，就会自动传递给每个节点
let info = new InfoLogger()
let debug = new DebugLogger()
let error = new ErrorLogger()
info.setNext(debug)
debug.setNext(error)
error.setNext(null)

info.logMsg(2,'发生了一个错误')
